package com.indusfo.spc.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.indusfo.spc.pojo.User;
import com.indusfo.spc.service.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author chenm
 * @create 2019-12-03 10:30
 */
@Service
public class TokenServiceImpl implements TokenService {
    @Override
    public String getToken(User user) {
        Date start  = new Date();
        //24小时有效时间
        long currentTime = System.currentTimeMillis() + 24*60* 60 * 1000;
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(user.getUserId().toString()).withIssuedAt(start).withExpiresAt(end).sign(Algorithm.HMAC256(user.getUserPwd()));
        return token;
    }
}
